#include <algorithm>
#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;
class Solution {
public:
    string minWindow(string s, string t) {
        unordered_map<char, int>need,windows;
        for(char c:t)need[c]++;
        int l=0,valid=0,r=0,len=INT_MAX,start=0;
        while(r<s.size())
        {
            char c=s[r];
            r++;
            if(need.count(c))
            {
                windows[c]++;
                if(windows[c]==need[c])
                {
                    valid++;
                }
            }
            while(valid==need.size())
            {
                if(len>r-l)
                {
                    start=l;
                    len=r-l;
                }
                char a=s[l];
                l++;
                if(need.count(a))
                {
                    windows[a]--;
                    if(windows[a]<need[a])
                    {
                        valid--;
                    }
                    
                }   
            }
        }
        return len==INT_MAX?"":s.substr(start,len);
    }
};
